package com.system.jfm.MainTables;
import com.mysql.cj.x.protobuf.MysqlxCrud;
import com.system.Daos.StudentScoreDao;
import com.system.Daos.StudentScoreDao;
import com.system.Models.Studentscore;
import com.system.Models.Studentscore;
import com.system.jfm.Components.*;
import com.system.jfm.MainTable;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;

public class StudentScore extends JPanel{
    TopPanel topPanel;
    // 表格
    JTable table;
    JScrollPane scrollPane;
    // 右键菜单
    RightMenu rightMenu;

    private void updateTableModel(DefaultTableModel model) {

        table.setModel(model);
        //设置列宽
        TableColumnModel columnModel = table.getColumnModel();
        TableColumn column1 = columnModel.getColumn(0);
        TableColumn column2 = columnModel.getColumn(1);
        TableColumn column3 = columnModel.getColumn(2);
        column1.setPreferredWidth(150);
        column2.setPreferredWidth(150);
        column3.setPreferredWidth(150);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setFillsViewportHeight(true);
    }
    public void initTable() throws SQLException {
        DefaultTableModel model = new StudentScoreDao().getAllTableModel();
        updateTableModel(model);
    }

    public StudentScore() throws SQLException {
        this.setBackground(Color.white);
        this.setLayout(new BorderLayout());

        rightMenu = new RightMenu();
        topPanel = new TopPanel();
        table = new JTable();

        topPanel.remove(topPanel.add_btn);
        rightMenu.remove(rightMenu.addItem);
        topPanel.remove(topPanel.delete_btn);
        rightMenu.remove(rightMenu.deleteItem);

        this.initTable();

        scrollPane = new JScrollPane(table);

        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                boolean flag = e.isPopupTrigger();
                if (flag) {
                    rightMenu.show(table, e.getX(), e.getY());
                }
            }
        });

        rightMenu.alterItem.addActionListener(alterStudentListener);
        topPanel.alter_btn.addActionListener(alterStudentListener);

        rightMenu.selectItem.addActionListener(searchStudentListener);
        topPanel.select_btn.addActionListener(searchStudentListener);

        topPanel.show_all.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    initTable();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        this.add(rightMenu);
        this.add(scrollPane, BorderLayout.CENTER);
        this.add(topPanel, BorderLayout.NORTH);
    }
    ActionListener alterStudentListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int selectRow = table.getSelectedRow();
            if (selectRow==-1){
                new Message("请选中后修改");
                return;
            }
            JTextField idField = new JTextField();
            JTextField mathField = new JTextField();
            JTextField englishField =new JTextField();
            Object[] message = {
                    "学号", idField,
                    "数学", mathField,
                    "英语", englishField,
            };
            String id = table.getValueAt(selectRow, 0).toString();
            String math = getScore(table.getValueAt(selectRow, 1));
            String english =getScore(table.getValueAt(selectRow, 2));
            idField.setText(id);
            mathField.setText(math.toString());
            englishField.setText(english.toString());
            idField.setEnabled(false);
            while (true) {
                int option = JOptionPane.showConfirmDialog(null, message, "修改", JOptionPane.OK_CANCEL_OPTION);
                if (option == JOptionPane.CANCEL_OPTION || option == JOptionPane.CLOSED_OPTION) {
                    break;
                }
                math = mathField.getText();
                english = englishField.getText();
                    if (option == JOptionPane.OK_OPTION) {
                        boolean flag;
                        try {
                            flag = new StudentScoreDao(new Studentscore(id, math,english)).alterStudentscore();
                            initTable();
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
                        if (flag) {
                            break;
                        } else {
                            new Message("修改失败");
                        }
                    }
                }
                }
    };
    ActionListener searchStudentListener=new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JTextField idField = new JTextField();
            RangeInputPanel mathRangePanel = new RangeInputPanel();
            RangeInputPanel englishRangePanel = new RangeInputPanel();

            Object[] message = {
                    "学号", idField,
                    "数学", mathRangePanel,
                    "英语", englishRangePanel,
            };
            int option = JOptionPane.showConfirmDialog(null, message, "查找", JOptionPane.OK_CANCEL_OPTION);
            String id = idField.getText();
            if (option == JOptionPane.OK_OPTION) {
                try {
                    DefaultTableModel model = new StudentScoreDao(new Studentscore(id,mathRangePanel.getRange(), englishRangePanel.getRange())).searchStudentscore();
                    updateTableModel(model);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }
    };
    private String getScore(Object obj){
        if(obj==null){
            return "";
        }else {
            return obj.toString();
        }
    }
}

